﻿using System;

using System.Xml.Serialization;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;

public class Common
{
    public class Utilities
    {
        /// <summary>
        /// Generates a random GUID
        /// </summary>
        /// <returns>a string object with the GUID</returns>
        public static string GenerateGUID()
        {
            return System.Guid.NewGuid().ToString();
        }

        /// <summary>
        /// Extracts all hash tags in a given text
        /// </summary>
        /// <param name="text">text in which to look for hashtags</param>
        /// <returns>list of string with hashtags found</returns>
        public static List<string> GetHashTagsInText(string text)
        {
            List<string> foundHashTags = new List<string>();
            Regex hashTagFinder = new Regex(Common.HashTagRegEx);
            MatchCollection foundHashTagsMatchCollection = hashTagFinder.Matches(text);
            for(int i = 0, il=foundHashTagsMatchCollection.Count;i<il;i++)
            {
                foundHashTags.Add(foundHashTagsMatchCollection[i].ToString());
            }
            return foundHashTags;
        }

    }

    /// <summary>
    /// Encryption methods
    /// </summary>
    public class Encoding
    {
        /// <summary>
        /// encodes a string 
        /// </summary>
        /// <param name="textToEncode"></param>
        /// <returns></returns>
        public static string EncodeString(string textToEncode)
        {
            // for now we'll do base 64 encoding instead of encrypting...
            return Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(textToEncode));
        }

        /// <summary>
        /// decodes a string
        /// </summary>
        /// <param name="textToDecode"></param>
        /// <returns></returns>
        public static string DecodeString(string textToDecode)
        {
            // for now we'll do base 64 encoding instead of encrypting...
            return System.Text.Encoding.ASCII.GetString(Convert.FromBase64String(textToDecode));
        }
    }

    /// <summary>
    /// An application user
    /// </summary>
    public class User
    {
        [XmlAttribute]
        public string userId = "";

        [XmlAttribute]
        public string userName = "";

        [XmlAttribute]
        public string password = "";

        [XmlAttribute]
        public string email = "";

        [XmlAttribute]
        public string phoneNumber = "";

        [XmlElement]
        public List<string> usersFollowed = new List<string>();
    }

    /// <summary>
    /// A tweet :)
    /// </summary>
    public class Tweet
    {
        [XmlAttribute]
        public string text = "";

        [XmlAttribute]
        public string timestamp = "";

        [XmlAttribute]
        public string userId = "";

        [XmlAttribute]
        public string tweetId = "";
    }

    public enum UserCreateStatus
    {
        Created,
        AlreadyExists,
        Error
    }

    public enum TweetCreateStatus
    {
        Created,
        UserDoesntExist,
        Error
    }

    public enum FollowUserStatus
    {
        AlreadyFollowing,
        Following
    }

    public static string UsersFile = "Users.xml";
    public static string TweetsFile = "Tweets.xml";
    public static string HashTagRegEx = @"\B#(\w*[A-Za-z_]+\w*)";
}